package com.yld.car.market;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.yld.car.application.MyApplication;
import com.yld.car.common.ConstantUtils;
import com.yld.car.common.NetworkProgressUtils;
import com.yld.car.common.UploadImageTask;
import com.yld.car.domain.BaseColorInfo;
import com.yld.car.domain.Car;
import com.yld.car.domain.DetailInfoCar;
import com.yld.car.domain.MyCarMarketInfo;
import com.yld.car.domain.SearchDetailCar;
import com.yld.car.domain.UserInfo;

public class MyCarPublishActivity extends BaseActivity implements
		OnItemClickListener {

	private ListView list1;
	private ListView list2;
	private ListView list3;
	private MyApplication app;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.publish_car);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		app = this.mApp;
		this.list1 = ((ListView) findViewById(R.id.list1));
		this.list1.setOnItemClickListener(this);

		this.list2 = ((ListView) findViewById(R.id.list2));
		this.list2.setOnItemClickListener(this);

		this.list3 = ((ListView) findViewById(R.id.list3));
		this.list3.setOnItemClickListener(this);

		Intent intent = getIntent();

		mApp.setmCarStateValue(-1);
		if (intent != null) {
			isEditor = intent.getBooleanExtra("isEditor", false);
			mDetailInfoCar = (DetailInfoCar) intent
					.getSerializableExtra("detailInfo");
		}
		TextView localTextView = (TextView) findViewById(R.id.title);

		if (isEditor) {
			localTextView.setText("编辑车源");

		} else {
			localTextView.setText("发布新车源");
		}

		// leftButton=(Button) findViewById(R.id.leftButton);
		showButton();

		rightButton = (Button) findViewById(R.id.rightButton);

		this.rightButton.setVisibility(View.VISIBLE);
		this.rightButton.setText("完成");
		this.rightButton.setOnClickListener(completeListener);

	}

	// 是否为编辑状态
	private boolean isEditor;

	// 获取当前编辑车源的详细信息
	private DetailInfoCar mDetailInfoCar;
	private Button rightButton;

	private List<Map<String, String>> allInputItems;
	// private List<Map<String, String>> allInputItems2;
	// private List<Map<String, String>> allInputItems3;
	private Map<String, String> map;

	private String color = "";
	private String insideColor = "";
	private String woodColor = "";
	private String lunGu = "";
	private String paiLiang = "";
	private String carType = "";

	private String carState = "";

	// 添加车架号
	private String vin = "";

	// 配置信息
	// private String peizhi="";
	private void initViews() {
		allInputItems = new ArrayList<Map<String, String>>();

		this.map = new HashMap<String, String>();
		this.map.put("id", "typeAndStatus");
		this.map.put("title", "选择车源类型");
		this.map.put("content", this.carType);
		this.allInputItems.add(map);
		this.map = new HashMap<String, String>();
		this.map.put("id", "model");
		this.map.put("title", "选择对应系列");
		this.map.put("content", this.mname);

		this.allInputItems.add(map);

		// 添加车源状态选项

		this.map = new HashMap<String, String>();
		this.map.put("id", "carState");
		this.map.put("title", "选择车源状态");
		this.map.put("content", this.carState);
		this.allInputItems.add(map);

		this.map = new HashMap<String, String>();
		this.map.put("id", "priceInput");
		this.map.put("title", "请输入价格");
		this.map.put("content", "");

		this.allInputItems.add(map);

		// 添加一项车架号
		this.map = new HashMap<String, String>();
		this.map.put("id", "vin");
		this.map.put("title", "请输入车架号");
		this.map.put("content", this.vin);
		this.allInputItems.add(map);

		map = new HashMap<String, String>();
		this.map.put("id", "color");
		this.map.put("title", "请选择外观颜色");
		this.map.put("content", this.color);
		this.allInputItems.add(this.map);

		this.map = new HashMap<String, String>();
		this.map.put("id", "insideColor");
		this.map.put("title", "请选择内饰颜色");
		this.map.put("content", this.insideColor);
		this.allInputItems.add(this.map);

		this.map = new HashMap<String, String>();
		// this.map.put("id", "vin");
		// this.map.put("title", "请输入车架号");
		// this.map.put("content", this.vin);
		// this.allInputItems.add(this.map);

		this.map.put("id", "lungu");
		this.map.put("title", "请选择轮毂");
		this.map.put("content", this.lunGu);
		this.allInputItems.add(this.map);
		// this.map = new HashMap<String, String>();
		// this.map.put("id", "woodColor");
		// this.map.put("title", "请选择木饰颜色");
		// this.map.put("content", this.woodColor);
		// this.allInputItems.add(this.map);

		MyPublishCarAdapter adapter = new MyPublishCarAdapter(this,
				allInputItems);

		list1.setAdapter(adapter);
		//

		this.allInputItems = new ArrayList<Map<String, String>>();

		// this.map = new HashMap<String, String>();
		// this.map.put("id", "paiLiang");
		// this.map.put("title", "请选择排量");
		// this.map.put("content", this.paiLiang);
		// this.allInputItems.add(this.map);

		this.map = new HashMap<String, String>();
		this.map.put("id", "peizhi");
		this.map.put("title", "请选择配置");
		this.map.put("content", this.peizhi);
		this.allInputItems.add(this.map);

		adapter = new MyPublishCarAdapter(this, allInputItems);
		this.list2.setAdapter(adapter);

		this.allInputItems = new ArrayList<Map<String, String>>();
		this.map = new HashMap<String, String>();
		this.map.put("content", "");
		this.map.put("id", "photo");
		this.map.put("title", "请上传图片");

		this.allInputItems.add(map);
		// this.map = new HashMap<String, String>();
		// this.map.put("id", "beizhu");
		// this.map.put("title", "请输入备注内容");
		// this.map.put("content", this.beizhu);
		//
		// this.allInputItems.add(map);

		adapter = new MyPublishCarAdapter(this, allInputItems);
		this.list3.setAdapter(adapter);

	}

	// private String formalities = "";
	private String peizhi = "";
	// private String vin = "";
	private String mname = "";
	private String beizhu = "";

	private void showButton() {

		Button localButton = (Button) findViewById(R.id.leftButton);
		localButton.setVisibility(View.VISIBLE);

		localButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				// clearData();
				// mApp.setmVinName(null);
				finish();

			}
		});
	}

	private void clearData() {
		mApp.setCar(null);
		mApp.setmSeriesCar(null);
		mApp.setmEnteriorColor(null);
		mApp.setmInnerColor(null);
		// mApp.setmWoodColor(null);
		mApp.setmWheelHubName(null);
		mApp.setConfigurationOneIds(null);
		mApp.setConfigurationTwoIds(null);
		mApp.setConfigurationThreeIds(null);
		mApp.setConfigurationFourIds(null);
		mApp.getAllConfigurations().clear();
		// mApp.setmDisplacementName();
		mApp.setPriceInput("");
		mApp.getAllImgPaths().clear();
		mApp.setmCarStateValue(-1);
		mApp.setmQHDate(null);
		mApp.setSaveVin("");
		mApp.setDefault(true);
	}

	View.OnClickListener completeListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			System.out.println("complete----->");
			AsyncTask<String, String, String> t = new AsyncTask<String, String, String>() {

				@Override
				protected String doInBackground(String... params) {

					publishProgress("");

					String result = publishCompleteState();
					System.out.println("result----->" + result);
					if (result == null) {
						result = "";
					}
					return result;

				}

				@Override
				protected void onPostExecute(String result) {

					// System.out.println("result==="+result);
					hideLoading();
					if (result != null && !result.equals("")
							&& !result.equals("error")) {

						// System.out.println("res");
						if (result.indexOf("添加成功") >= 0) {

							showDialogFinish("发布成功", true);
						} else {
							Toast.makeText(getApplicationContext(),
									"发布失败，请稍候重试", Toast.LENGTH_SHORT).show();
						}
					} else {
						if (!result.equals("error")) {
							Toast.makeText(getApplicationContext(),
									"发布车源参数设置有误，请重试", Toast.LENGTH_SHORT)
									.show();
						}

					}

					super.onPreExecute();
				}

				@Override
				protected void onProgressUpdate(String... values) {
					showLoading("");

					super.onProgressUpdate(values);
				}

			};
			t.execute("");
		}
	};

	private class MyPublishCarAdapter extends BaseAdapter {

		private Context mContext;
		private List<Map<String, String>> mAllInputItems;

		public MyPublishCarAdapter(Context context,
				List<Map<String, String>> allInputItems) {

			this.mContext = context;
			this.mAllInputItems = allInputItems;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return this.mAllInputItems.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return this.mAllInputItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			// System.out.println("getView======>" + this);
			String type = mAllInputItems.get(position).get("id");

			if ((type.equals("typeAndStatus")) || (type.equals("model"))
					|| (type.equals("color")) || (type.equals("insideColor"))
					|| "carState".equals(type)) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.base_list_items_simple, null);
				TextView textView = (TextView) convertView
						.findViewById(R.id.itemContent);
				textView.setHintTextColor(this.mContext.getResources()
						.getColorStateList(R.color.red_config));
				textView.setHint(this.mAllInputItems.get(position).get("title"));
				textView.setTextColor(this.mContext.getResources()
						.getColorStateList(R.color.font_blue_deep));
				textView.setText(this.mAllInputItems.get(position).get(
						"content"));

			}
			if ("photo".equals(type)) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.base_list_items_simple, null);
				TextView tx = (TextView) convertView
						.findViewById(R.id.itemContent);
				tx.setTextColor(this.mContext.getResources().getColorStateList(
						R.color.font_gray_light));
				tx.setText(mAllInputItems.get(position).get("title"));

			}

			if ("priceInput".equals(type)) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.publish_car_items_input_price_n, null);

				EditText localEditText3 = (EditText) convertView
						.findViewById(R.id.itemContent);
				localEditText3.setHint(this.mAllInputItems.get(position).get(
						"title"));

				String price = mApp.getPriceInput();
				if (price == null) {
					price = "";
				}
				localEditText3.setText(price);

				// 给价格输入框设置监听事件

				localEditText3.addTextChangedListener(new TextWatcher() {

					@Override
					public void onTextChanged(CharSequence s, int start,
							int before, int count) {

						mApp.setPriceInput(s.toString());

					}

					@Override
					public void beforeTextChanged(CharSequence s, int start,
							int count, int after) {
						// TODO Auto-generated method stub

					}

					@Override
					public void afterTextChanged(Editable s) {
						// TODO Auto-generated method stub

					}
				});

			}

			if ("vin".equals(type)) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.publish_car_items_input_vin_n, null);

				EditText localEditText3 = (EditText) convertView
						.findViewById(R.id.itemContent);
				localEditText3.setHint(this.mAllInputItems.get(position).get(
						"title"));

				String vin = mApp.getSaveVin();
				if (vin == null) {
					vin = "";
				}
				localEditText3.setText(vin);

				// 给价格输入框设置监听事件

				localEditText3.addTextChangedListener(new TextWatcher() {

					@Override
					public void onTextChanged(CharSequence s, int start,
							int before, int count) {

						mApp.setSaveVin(s.toString());

					}

					@Override
					public void beforeTextChanged(CharSequence s, int start,
							int count, int after) {
						// TODO Auto-generated method stub

					}

					@Override
					public void afterTextChanged(Editable s) {
						// TODO Auto-generated method stub

					}
				});

			}
			// if (type.equals("vin")) {
			// convertView = LayoutInflater.from(mContext).inflate(
			// R.layout.base_list_items_input, null);
			// EditText localEditText2 = (EditText) convertView
			// .findViewById(R.id.itemUserName);
			// localEditText2.setHint(this.mAllInputItems.get(position).get(
			// "title"));
			// localEditText2.setTextColor(this.mContext.getResources()
			// .getColorStateList(R.color.font_blue_deep));
			// localEditText2.setText(this.mAllInputItems.get(position).get(
			// "content"));
			//
			// }

			if ("lungu".equals(type)) {

				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.base_list_items_simple, null);
				TextView textView = (TextView) convertView
						.findViewById(R.id.itemContent);
				textView.setHintTextColor(this.mContext.getResources()
						.getColorStateList(R.color.font_gray_light_2));
				textView.setHint(this.mAllInputItems.get(position).get("title"));
				textView.setTextColor(this.mContext.getResources()
						.getColorStateList(R.color.font_gray_light_2));
				textView.setText(this.mAllInputItems.get(position).get(
						"content"));

			}

			if ("peizhi".equals(type)) {

				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.config_list_item, null);
				TextView textView = (TextView) convertView
						.findViewById(R.id.itemContent);
				textView.setHintTextColor(this.mContext.getResources()
						.getColorStateList(R.color.font_gray_light_2));
				textView.setHint(this.mAllInputItems.get(position).get("title"));
				textView.setTextColor(this.mContext.getResources()
						.getColorStateList(R.color.font_gray_light_2));
				textView.setText(this.mAllInputItems.get(position).get(
						"content"));
				convertView.setMinimumHeight(480);
			}

			// if ("beizhu".equals(type)) {
			// //
			// convertView = LayoutInflater.from(mContext).inflate(
			// R.layout.base_list_items_input_large, null);
			// EditText localEditText1 = (EditText) convertView
			// .findViewById(R.id.itemContent);
			// localEditText1.setHint(this.mAllInputItems.get(position).get(
			// "title"));
			// localEditText1.setTextColor(this.mContext.getResources()
			// .getColorStateList(R.color.blue_config));
			// localEditText1.setText(this.mAllInputItems.get(position).get(
			// "content"));
			//
			// }
			convertView
					.setBackgroundResource(android.R.drawable.list_selector_background);
			return convertView;
		}

		@Override
		public boolean isEnabled(int position) {

			String str = this.mAllInputItems.get(position).get("id");

			if (isEditor) {

				if ((str.equals("typeAndStatus")) || (str.equals("model"))) {
					return false;
				} else {
					return super.isEnabled(position);
				}
			}
			if ((str.equals("vin")) || (str.equals("beizhu"))) {
				return false;
			} else {
				return super.isEnabled(position);
			}

		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		Car c = app.getCar();
		if (c != null) {
			this.carType = c.getName();
		} else {

		}

		c = app.getmSeriesCar();
		if (c != null) {
			this.mname = c.getName();
		}
		// System.out.println(app + ",,onResme==");

		if (app.getmEnteriorColor() != null) {
			String exterior = app.getmEnteriorColor().get("name");
			if (exterior != null) {
				this.color = exterior;
			}
		}

		if (app.getmInnerColor() != null) {
			String interior = app.getmInnerColor().get("name");
			if (interior != null) {
				this.insideColor = interior;
			}
		}

		if (app.getmWoodColor() != null) {
			String woodColor = app.getmWoodColor().get("name");
			if (woodColor != null) {
				this.woodColor = woodColor;
			}
		}

		if (app.getmWheelHubName() != null) {
			String wheelHub = app.getmWheelHubName().get("name");

			if (wheelHub != null) {

				this.lunGu = wheelHub;
			}
		}

		String vinName = app.getmDisplacementName();

		if (vinName != null) {
			this.paiLiang = vinName;
		}
		int type = mApp.getmCarStateValue();
		if (type == 0) {
			this.carState = "现货";
		} else if (type == 1) {
			// "期货-"+car.getQhtime() + "-到港";
			this.carState = "期货-" + mApp.getmQHDate() + "-到港";
		}

		progressConfigInfo();
		this.initViews();
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long id) {

		String key = ((HashMap<String, String>) adapterView.getAdapter()
				.getItem(position)).get("id");

		if ("typeAndStatus".equals(key)) {
			app.setmSeriesCar(null);
			this.mname = "";
			Intent intent = new Intent();
			intent.setClass(this, PublishCarPickTypeAndStatusActivity.class);
			startActivity(intent);

		} else if ("model".equals(key)) {

			startNewActivity(CarSeriesActivity.class, true, "请选择车源类型",
					mApp.getCar());
		} else if ("color".equals(key)) {

			startNewActivity(ExteriorActivity.class, false, "请选择车系列",
					mApp.getmSeriesCar());
		} else if ("insideColor".equals(key)) {

			startNewActivity(InteriorActivity.class, false, "请选择车系列",
					mApp.getmSeriesCar());
		} else if ("woodColor".equals(key)) {

			startNewActivity(WoodColorActivity.class, false, "请选择车系列",
					mApp.getmSeriesCar());
		} else if ("lungu".equals(key)) {

			startNewActivity(WheelHubActivity.class, false, "请选择车系列",
					mApp.getmSeriesCar());
		} else if ("paiLiang".equals(key)) {

			startNewActivity(DisplacementActivity.class, false, "请选择车系列",
					mApp.getmSeriesCar());
		} else if ("peizhi".equals(key)) {

			startNewActivity(ConfigurationActivity.class, false, "请选择车系列",
					mApp.getmSeriesCar());
		} else if ("photo".equals(key)) {

			Intent intent = new Intent();
			intent.setClass(MyCarPublishActivity.this,
					PublishCarPickPhotoActivity.class);
			startActivity(intent);
		} else if ("carState".equals(key)) {

			//
			// startNewActivity(SelectorCarStateActivity.class, false, "请选择车系列",
			// mApp.getmSeriesCar());

			Intent intent = new Intent();
			intent.setClass(MyCarPublishActivity.this,
					SelectorCarStateActivity.class);
			startActivity(intent);
		}

	}

	private void startNewActivity(Class classz, boolean isFlag, String prompt,
			Car c) {

		if (c != null) {
			String cId = c.getId();

			Intent intent = new Intent();
			intent.putExtra("cId", cId);
			if (isFlag) {
				intent.putExtra("flag", true);
			}

			intent.setClass(this, classz);
			startActivity(intent);

		} else {
			this.showDialogFinish(prompt, false);

		}
	}

	/**
	 * 添加车辆信息(即发布车源) AddNewCars(string seriesId, string title, string userId,
	 * string vin, string configuration, string configurationIDs, string
	 * outerExteriorID, string innerColorID, string displacement, string
	 * seating, string smallImg, string woodColorID, string wheelHubID, string
	 * press, string type, string typeTime)
	 * 
	 * 
	 * 参数一：seriesId: 系列ID 参数二：title: 标题（即外内饰简称,如“外黑内白”） 参数三：userId: 当前操作的用户ID
	 * 参数四：vin:车架号 参数五：configuration: 选配配置，多个配置以 ¤ 分割 格式如： 运动套装¤后视摄像头¤加热方向盘
	 * 参数六：configurationIDs: 选配配置ID，多个配置id以,分割 格式如： ,629,660,2061,2066
	 * 参数七：outerExteriorID : 外饰ID 参数八：innerColorID : 内饰ID 参数九：displacement : 排量
	 * 格式如： 3.0L 参数十：seating: 座位数 格式如： 5 参数十一：smallImg : 车辆图片
	 * 格式如：201401181006571972.jpg 当前年月日时分秒毫秒
	 * 多个图片以小写的“；”分割(为了图片能够在pc端正常显示，需将图片大小设置为450X390，大小在100kb以下)
	 * 参数十二：woodColorID: 木饰ID 参数十三：wheelHubID: 轮毂ID 参数十四：press : 价格（万）
	 * 参数十五：type: 期货还是现货(传入值为：“期货”，“现货”) 参数十六：typeTime: 期货到港时间（现货为空） 只需要年月日
	 * 
	 * 添加成功，返回：
	 * 
	 * { "result":"1",
	 */

	public String publishCompleteState() {

		// 封装参数

		HashMap<String, String> allParams = new HashMap<String, String>();

		if (mApp.getCar() == null || mApp.getmSeriesCar() == null) {
			mHandler.sendEmptyMessage(0);

			return "error";
		}
		if (this.carState == null || "".equals(this.carState)) {
			mHandler.sendEmptyMessage(1);
			return "error";
		}
		Car c = this.mApp.getmSeriesCar();
		allParams.put("seriesId", c.getId());
		System.out.println("seriesId==" + c.getId());
		ArrayList<Car> ac = mApp.getAllDetails();

		if (ac == null || ac.size() == 0) {
			return null;
		}

		System.out.println(ac.size() + ".." + ac);
		Car lCar = ac.get(0);
		SearchDetailCar sdc = (SearchDetailCar) lCar;

		if (mApp.getmEnteriorColor() != null && mApp.getmInnerColor() != null) {

			String shortname1 = mApp.getmEnteriorColor().get("shortname");

			String shortname2 = mApp.getmInnerColor().get("shortname");
			allParams.put("title", "外" + shortname1 + "内" + shortname2);
		} else {
			allParams.put("title", "");
		}

		// System.out.println("title=" + sdc.getName());
		String userId = ((UserInfo) readObject("userInfo")).getUserId();
		allParams.put("userId", userId);
		// System.out.println("userId=" + ((UserInfo)
		// readObject()).getUserId());

		String vin = mApp.getSaveVin();
		if (vin == null || vin.length() == 0) {
			vin = "10101010";
		}

		allParams.put("vin", vin);
		// System.out.println("vin=" + sdc.getVin());
		HashMap<String, ArrayList<BaseColorInfo>> allItems = mApp
				.getAllConfigurations();
		// System.out.println("-----configuration-->");
		if (allItems == null || allItems.size() == 0) {
			return null;
		}

		boolean isSelect = mApp.isSelect();
		if (isSelect) {

			ArrayList<BaseColorInfo> bc1 = allItems
					.get(ConfigurationActivity.KEY1);
			ArrayList<BaseColorInfo> bc2 = allItems
					.get(ConfigurationActivity.KEY2);
			ArrayList<BaseColorInfo> bc3 = allItems
					.get(ConfigurationActivity.KEY3);
			ArrayList<BaseColorInfo> bc4 = allItems
					.get(ConfigurationActivity.KEY4);
			// ArrayList<String> allItems1 = new ArrayList<String>();
			// ArrayList<String> allItems2 = new ArrayList<String>();
			StringBuffer sb1 = new StringBuffer();
			StringBuffer configIdsOne = new StringBuffer();
			StringBuffer configIdsTwo = new StringBuffer();
			StringBuffer configIdsThree = new StringBuffer();
			StringBuffer configIdsFour = new StringBuffer();
			long[] checksOne = mApp.getConfigurationOneIds();
			long[] checksTwo = mApp.getConfigurationTwoIds();
			long[] checksThree = mApp.getConfigurationThreeIds();
			long[] checksFour = mApp.getConfigurationThreeIds();

			if (checksOne == null) {
				checksOne = new long[0];
			}
			if (checksTwo == null) {
				checksTwo = new long[0];
			}
			if (checksThree == null) {
				checksThree = new long[0];
			}
			if (checksFour == null) {
				checksFour = new long[0];
			}
			for (int i = 0; i < checksOne.length; i++) {

				BaseColorInfo b = bc1.get((int) checksOne[i]);
				// allItems1.add(b.getDdname());
				sb1.append(b.getDdname()).append("¤");

				configIdsOne.append(",").append(b.getId());

			}
			configIdsOne.append(",");
			StringBuffer sb2 = new StringBuffer();
			for (int i = 0; i < checksTwo.length; i++) {

				BaseColorInfo b = bc2.get((int) checksTwo[i]);
				sb2.append(b.getDdname());

				configIdsTwo.append(b.getId());
				if ((i == checksTwo.length - 1)) {
					sb2.append("");
					configIdsTwo.append("");
				} else {
					sb2.append("¤");
					configIdsTwo.append(",");
				}

				// allItems2.add(b.getDdname());

			}

			StringBuffer sb3 = new StringBuffer();
			for (int i = 0; i < checksThree.length; i++) {

				BaseColorInfo b = bc3.get((int) checksThree[i]);
				sb3.append("¤").append(b.getDdname());
				configIdsThree.append(",").append(b.getId());
			}

			StringBuffer sb4 = new StringBuffer();

			for (int i = 0; i < checksFour.length; i++) {

				BaseColorInfo b = bc4.get((int) checksFour[i]);
				sb4.append("¤").append(b.getDdname());
				configIdsFour.append(",").append(b.getId());
			}
			// configIdsFour.append(",");
			configIdsThree.append(configIdsFour);
			configIdsTwo.append(configIdsThree);
			sb3.append(sb4);
			sb2.append(sb3);
			sb1.append(sb2);
			configIdsOne.append(configIdsTwo);
			allParams.put("configuration", sb1.toString());// 配置信息
			// System.out.println("configuration=" + sb1.toString());
			allParams.put("configurationIDs", configIdsOne.toString());
		} else {
			allParams.put("configuration", "");// 配置信息
			// System.out.println("configuration=" + sb1.toString());
			allParams.put("configurationIDs", "");
		}
		// System.out.println("config---"+configIdsOne.toString());
		if (mApp.getmEnteriorColor() == null || mApp.getmInnerColor() == null
				|| mApp.getmWheelHubName() == null) {

			return null;
		}
		String enteriorId = mApp.getmEnteriorColor().get("id");
		// System.out.println("enteriorId=" + enteriorId);
		allParams.put("outerExteriorID", enteriorId);// 外饰id
		String innerColorId = mApp.getmInnerColor().get("id");
		// System.out.println("innerColorId=" + innerColorId);
		allParams.put("innerColorID", innerColorId);// 内饰id

		/*
		 * 参数九：displacement : 排量 格式如： 3.0L 参数十：seating: 座位数 格式如： 5 参数十一：smallImg
		 * : 车辆图片 格式如：201401181006571972.jpg 当前年月日时分秒毫秒
		 * 多个图片以小写的“；”分割(为了图片能够在pc端正常显示，需将图片大小设置为450X390，大小在100kb以下)
		 * 参数十二：woodColorID: 木饰ID 参数十三：wheelHubID: 轮毂ID 参数十四：press : 价格（万）
		 * 参数十五：type: 期货还是现货(传入值为：“期货”，“现货”) 参数十六：typeTime: 期货到港时间（现货为空） 只需要年月日
		 */
		// HashMap<String, ArrayList<>>
		// allParams.put("configuration", value)
		if (mApp.getmDisplacementName() == null) {
			// return null;
			mApp.setmDisplacementName("");
		}
		allParams.put("displacement", mApp.getmDisplacementName());
		System.out.println("displacement=" + mApp.getmDisplacementName() + "D");
		// System.out.println("seating=="+5);
		allParams.put("seating", "5");// 座位数
		// System.out.println("----->app");

		// 确定是否为系统提供的图片
		boolean isDefault = mApp.isDefault();
		if (isDefault) {
			allParams.put("smallImg", "");
			mApp.setDefault(false);
		} else {

			HashMap<String, String> allImgs = mApp.getAllImgPaths();
			if (allImgs == null || allImgs.size() == 0) {
				// System.out.println("nulll");
				return null;
			}
			ArrayList<String> allImgNames = mApp.getAllImgNames();
			if (allImgs != null && allImgs.size() != 0) {
				for (int i = 0; i < allImgs.size(); i++) {
					String name = allImgs.get("" + (i + 1));
					String fileName = name.substring(name.lastIndexOf("/") + 1,
							name.length());
					allImgNames.add(fileName);
					new UploadImageTask(name, fileName).execute("");

				}

			}

			StringBuffer imgSplits = new StringBuffer();
			for (int i = 0; i < allImgNames.size(); i++) {
				imgSplits.append(allImgNames.get(i)).append(";");

			}
			allParams.put("smallImg", imgSplits.toString());
		}

		// System.out.println("smallImg=" + imgSplits.toString());

		String woodColorId = mApp.getmWoodColor().get("id");
		if (woodColorId == null) {
			woodColorId = "";
		}
		allParams.put("woodColorID", woodColorId);
		System.out.println("woodColorId=" + woodColorId + "W");
		String wheelHubId = mApp.getmWheelHubName().get("id");
		// System.out.println("wheelHubID=" + wheelHubId);
		allParams.put("wheelHubID", wheelHubId);
		// System.out.println("press==" + 110);
		String press = mApp.getPriceInput();
		if (press == null || press.length() == 0) {
			press = "88";
		}
		// System.out.println("press==" + press);
		allParams.put("press", press);
		// System.out.println("type===" + sdc.getXhorgh());

		
		// System.out.println("typeTime==" + sdc.getQhtime());
		String typeTime = null;
		if (this.carState.indexOf("现货")>=0) {
			typeTime = "";
			this.carState="现货";
		} else if (this.carState.indexOf("期货")>=0) {
			typeTime = mApp.getmQHDate();
			this.carState="期货";
		}
		allParams.put("type", this.carState);
		allParams.put("typeTime", typeTime);
		NetworkProgressUtils utils = NetworkProgressUtils.getInstance();
		Object o = utils.getWebservicesByData(
				ConstantUtils.NAME_SPACE,
				ConstantUtils.ALL_METHROD_NAME.get(10),
				ConstantUtils.NAME_SPACE
						+ ConstantUtils.ALL_METHROD_NAME.get(10),
				ConstantUtils.GET_ADD_NEW_CARS_URL, allParams);
		// System.out.println(o);
		MyCarMarketInfo myCarMarketInfo = new MyCarMarketInfo();
		myCarMarketInfo.setBrandName(sdc.getPinpainame());
		myCarMarketInfo.setUeserId(userId);
		myCarMarketInfo.setNowPage(1);
		myCarMarketInfo.setPageSize(20);
		writeObject(myCarMarketInfo, "myCarMarketInfo");

		return o.toString();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		clearData();
	}

	private Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			int what = msg.what;
			String tmp = "";
			switch (what) {
			case 0:
				tmp = "没有确定车源类型或车源系列，不能发布车源";
				break;

			case 1:
				tmp = "请选择车源状态";
				break;
			}
			Toast.makeText(MyCarPublishActivity.this, tmp, Toast.LENGTH_SHORT)
					.show();
		}
	};

	private void progressConfigInfo() {

		HashMap<String, ArrayList<BaseColorInfo>> allItems = mApp
				.getAllConfigurations();
		// System.out.println("-----configuration-->");

		if (allItems != null && allItems.size() > 0) {

			boolean isSelect = mApp.isSelect();
			if (isSelect) {

				ArrayList<BaseColorInfo> bc1 = allItems
						.get(ConfigurationActivity.KEY1);
				ArrayList<BaseColorInfo> bc2 = allItems
						.get(ConfigurationActivity.KEY2);
				ArrayList<BaseColorInfo> bc3 = allItems
						.get(ConfigurationActivity.KEY3);
				ArrayList<BaseColorInfo> bc4 = allItems
						.get(ConfigurationActivity.KEY4);
				// ArrayList<String> allItems1 = new ArrayList<String>();
				// ArrayList<String> allItems2 = new ArrayList<String>();
				StringBuffer sb1 = new StringBuffer();
				StringBuffer configIdsOne = new StringBuffer();
				StringBuffer configIdsTwo = new StringBuffer();
				StringBuffer configIdsThree = new StringBuffer();
				StringBuffer configIdsFour = new StringBuffer();

				long[] checksOne = mApp.getConfigurationOneIds();
				long[] checksTwo = mApp.getConfigurationTwoIds();
				long[] checksThree = mApp.getConfigurationThreeIds();
				long[] checksFour = mApp.getConfigurationFourIds();

				if (checksOne == null) {
					checksOne = new long[0];
				}
				if (checksTwo == null) {
					checksTwo = new long[0];
				}
				if (checksThree == null) {
					checksThree = new long[0];
				}
				if (checksFour == null) {
					checksFour = new long[0];
				}
				for (int i = 0; i < checksOne.length; i++) {

					BaseColorInfo b = bc1.get((int) checksOne[i]);
					// allItems1.add(b.getDdname());
					sb1.append(b.getDdname()).append("¤");

					configIdsOne.append(",").append(b.getId());

				}
				configIdsOne.append(",");
				StringBuffer sb2 = new StringBuffer();
				for (int i = 0; i < checksTwo.length; i++) {

					BaseColorInfo b = bc2.get((int) checksTwo[i]);
					sb2.append(b.getDdname());

					configIdsTwo.append(b.getId());
					if ((i == checksTwo.length - 1)) {
						sb2.append("");
						configIdsTwo.append("");
					} else {
						sb2.append("¤");
						configIdsTwo.append(",");
					}

					// allItems2.add(b.getDdname());

				}

				StringBuffer sb3 = new StringBuffer();
				for (int i = 0; i < checksThree.length; i++) {

					BaseColorInfo b = bc3.get((int) checksThree[i]);
					sb3.append("¤").append(b.getDdname());
					configIdsThree.append(",").append(b.getId());
				}

				StringBuffer sb4 = new StringBuffer();

				for (int i = 0; i < checksFour.length; i++) {

					BaseColorInfo b = bc4.get((int) checksFour[i]);
					sb4.append("¤").append(b.getDdname());
					configIdsFour.append(",").append(b.getId());
				}
				// configIdsFour.append(",");
				configIdsThree.append(configIdsFour);
				configIdsTwo.append(configIdsThree);
				sb3.append(sb4);
				sb2.append(sb3);
				sb1.append(sb2);
				configIdsOne.append(configIdsTwo);

				this.peizhi = sb1.toString();
				// allParams.put("configuration", sb1.toString());// 配置信息
				// System.out.println("configuration=" + sb1.toString());
				// allParams.put("configurationIDs", configIdsOne.toString());
			} else {

				this.peizhi = "";
			}
		}
	}
}
